package com.csx.queue;

import com.csx.array.Array;

/**
 * 数组队列
 * @author 陈胤训
 * <p>
 * create: 2019-04-02 11:17
 **/
public class ArrayQueue<E> implements Queue<E>{

    private Array<E> array;

    public ArrayQueue(int capacity){
        array = new Array<>(capacity);
    }

    public ArrayQueue(){
        array = new Array<>();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void enqueue(E e) {
        array.addLast(e);
    }

    @Override
    public E dequeue() {
        return array.deleteFirst();
    }

    @Override
    public E getFront() {
        return array.getFirst();
    }

    /** 查看队列大小  */
    public int getCapacity(){
        return array.getCapacity();
    }

    /**
     * 重写toString方法
     * @return
     */
    @Override
    public String toString(){
        StringBuilder builder = new StringBuilder();
        builder.append("Queue: ");
        builder.append("front {");
        for (int i = 0 ; i < array.getSize() ; i ++){
            builder.append(array.get(i));
            if ( i != array.getSize() - 1){
                builder.append(",");
            }
        }
        builder.append("} tail");
        return builder.toString();
    }

    public static void main(String[] args) {

        ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
        for (int i = 0; i < 10 ; i++) {
            arrayQueue.enqueue(i);
            System.out.println(arrayQueue);
            if (i % 3 == 2){
                System.out.println(arrayQueue.dequeue());
            }
        }

    }
}
